import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:flutter_demo_app/http/HttpController.dart';

class HomeworkListPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => HomeworkListState();
}

class HomeworkListState extends State<HomeworkListPage>
    with SingleTickerProviderStateMixin {
  var homeTabsController;
  var homeworkList = new List();

  Future getHomeworkData() async {
    print('getHomeworkData');
    setState(() {
      loadHomeworkList();
    });
  }

  loadHomeworkList() {
    print('loadHomeworkList');
    Map<String, String> map = new Map();
    map['userId'] = '600957';
    map['workType'] = '0';
    map['pageIndex'] = '1';
    map['pageSize'] = '10';

    HttpController.get('https://www.bettermorph.com/micoservcice/user/homework',
        (data) {
      if (data != null) {
        print(data);
        final body = json.decode(data.toString()); //接口返回的数据
        final feeds = body['obj'];
        if (body['code'] == 0) {
          //更新变量值
          setState(() {
            homeworkList = feeds;
            print(homeworkList);
          });
        }
      }
    }, params: map);
  }

  @override
  void initState() {
    homeTabsController = TabController(
      length: homeworkFinishType.length,
      vsync: this, //动画效果的异步处理，默认格式，背下来即可
    );
    super.initState();
    getHomeworkData();
    print('initState');
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        backgroundColor: Colors.deepOrange,
        title: new Text(
          '我的作业',
          style: new TextStyle(
              color: Colors.white,
              fontSize: 18.0,
              fontWeight: FontWeight.normal),
        ),
        bottom: new TabBar(
          isScrollable: false,
          controller: homeTabsController,
          indicatorWeight: 2.0,
          indicatorColor: Colors.white,
          tabs: homeworkFinishType.map((Choice choice) {
            return Tab(text: choice.title, icon: Icon(choice.icon));
          }).toList(),
        ),
      ),
      body: new TabBarView(
        controller: homeTabsController,
        // children: homeworkFinishType.map((Choice choice) {
        //   return new Center(child: new Text(choice.title));
        // }).toList(),
        children: <Widget>[
          // new Center(
          //   child: new FutureBuilder<DataModel>(
          //     future: loadHomeworkList(),
          //     builder: (context,homework){
          //       return new Text("${DataModel.homework}");
          //     },
          //   ),
          // )
          // homeworkAll(homeworkList),
          new Center(child: new Text('全部作业')),
          new Center(child: new Text('待完成')),
          new Center(child: new Text('已完成'))
        ],
      ),
    );
  }

  Widget homeworkAll(homeworkList) {
    // List<Widget> tagList = [];

    // for(var item in homeworkList){
    //     tagList.add(
    //       new Column(
    //         children: <Widget>[
    //           Text('quanbu ')
    //           // Container(child: new Text('${item["taskContent"]}'),)
    //         ],
    //       )
    //     );
    // }
  }
}

class Choice {
  const Choice({this.title, this.icon});
  final String title;
  final IconData icon;
}

const List<Choice> homeworkFinishType = const <Choice>[
  const Choice(title: '全部作业', icon: Icons.sentiment_satisfied),
  const Choice(title: '待完成', icon: Icons.sentiment_very_dissatisfied),
  const Choice(title: '已完成', icon: Icons.sentiment_very_satisfied)
];

class DataModel {
  final String taskContent;

  DataModel({this.taskContent});

  factory DataModel.fromJson(Map<String, dynamic> json) {
    return DataModel(taskContent: json['taskContent']);
  }
}
